//
//  NSString+YJCategory.m
//  Rail
//
//  Created by hychen on 2018/2/26.
//  Copyright © 2018年 Hychen. All rights reserved.
//

#import "NSString+YJCategory.h"
#import <CommonCrypto/CommonHMAC.h>

@implementation NSString (YJCategory)
// 转为MD5
- (NSString *)MD5_lib
{
    const char * cStr = [self UTF8String];
    unsigned char result[16];
    CC_MD5( cStr, (CC_LONG)strlen(cStr), result ); // This is the md5 call
    return [NSString stringWithFormat:
            @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
            result[0], result[1], result[2], result[3],
            result[4], result[5], result[6], result[7],
            result[8], result[9], result[10], result[11],
            result[12], result[13], result[14], result[15]
            ];
}

// 根据字符串生成NSURL链接
- (NSURL *)toURL_lib
{
    NSURL *url = [NSURL URLWithString:self];
    return url;
}

// 根据字符串主bundle生成NSURL链接
- (NSURL *)toMainBundleURL_libWithExt:(NSString *)ext
{
    return [[NSBundle mainBundle] URLForResource:self withExtension:ext];
}

// 自动追加_selected
- (NSString *)toSelectedImageName
{
    return [NSString stringWithFormat:@"%@_selected",self];
}

// 自动追加_highlighted
- (NSString *)toHighlightedImageName
{
    return [NSString stringWithFormat:@"%@_highlighted",self];
}

// 自动追加_disable
- (NSString *)toDisableImageName
{
    return [NSString stringWithFormat:@"%@_disabled",self];
}


// 计算文本所占的尺寸
- (CGSize)sizeWithMaxSize:(CGSize)maxsize fontSize:(CGFloat)fontSize
{
    return [self boundingRectWithSize:maxsize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:fontSize]} context:nil].size;
}

// 去除emoji表情
- (NSString *)stringWithoutEmoji
{
    __block NSString *result = self;
    [self enumerateSubstringsInRange:NSMakeRange(0, [self length]) options:NSStringEnumerationByComposedCharacterSequences usingBlock:
     ^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
         const unichar hs = [substring characterAtIndex:0];
         // surrogate pair
         if (0xd800 <= hs && hs <= 0xdbff) {
             if (substring.length > 1) {
                 const unichar ls = [substring characterAtIndex:1];
                 const int uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000;
                 if (0x1d000 <= uc && uc <= 0x1f77f) {
                     result = [result stringByReplacingOccurrencesOfString:substring withString:@""];
                 }
             }
         } else if (substring.length > 1) {
             const unichar ls = [substring characterAtIndex:1];
             if (ls == 0x20e3) {
                 result = [result stringByReplacingOccurrencesOfString:substring withString:@""];
             }
             
         } else {
             // non surrogate
             if (0x2100 <= hs && hs <= 0x27ff) {
                 result = [result stringByReplacingOccurrencesOfString:substring withString:@""];
             } else if (0x2B05 <= hs && hs <= 0x2b07) {
                 result = [result stringByReplacingOccurrencesOfString:substring withString:@""];
             } else if (0x2934 <= hs && hs <= 0x2935) {
                 result = [result stringByReplacingOccurrencesOfString:substring withString:@""];
             } else if (0x3297 <= hs && hs <= 0x3299) {
                 result = [result stringByReplacingOccurrencesOfString:substring withString:@""];
             } else if (hs == 0xa9 || hs == 0xae || hs == 0x303d || hs == 0x3030 || hs == 0x2b55 || hs == 0x2b1c || hs == 0x2b1b || hs == 0x2b50) {
                 result = [result stringByReplacingOccurrencesOfString:substring withString:@""];
             }
         }
     }];
    
    return result;
}

// 判断是否有空格
- (BOOL)hasWhiteSpace{
    NSRange range = [self rangeOfString:@" "];
    if (range.location != NSNotFound) {
        return YES;
    }else {
        return NO;
    }
}

- (NSString *)subStringFromBetweenStartString:(NSString *)start endString:(NSString *)end{
    if (self == nil) {
        return nil;
    }
    
    NSRange startRange = [self rangeOfString:start];
    NSRange endRange = [self rangeOfString:end];
    if (startRange.location != NSNotFound && endRange.location != NSNotFound) {
        NSRange range = NSMakeRange(startRange.location + startRange.length,endRange.location- startRange.location- startRange.length);
        if (range.location != NSNotFound) {
            return [self substringWithRange:range];
        }
    }
    return nil;
}

- (NSDate *)YJ_dateWithYYYYMMDDHHmmsss {
    return [self YJ_dateWithFormatter:@"yyyy-MM-dd HH:mm:ss"];
}

- (NSDate *)YJ_dateWithFormatter:(NSString *)format {
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:format];
    NSDate *date = [formatter dateFromString:self];
    return date;
    
}

- (BOOL)isNotEmptyString {
    if ([self isKindOfClass:[NSString class]] && self.length > 0) {
        return YES;
    }
    return NO;
}
@end
